package com.qf.fmall2202.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2202.product.dao.ProductDao;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.entity.ProductSku;
import com.qf.fmall2202.product.service.ProductImgService;
import com.qf.fmall2202.product.service.ProductService;
import com.qf.fmall2202.product.service.ProductSkuService;
import com.qf.fmall2202.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author 徐英海
 * @since 2022-11-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductService {
    @Autowired
    private ProductImgService productImgService;
    @Autowired
    private ProductSkuService productSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryListRecommends() {
        List<Product> productList = this.list(
                new QueryWrapper<Product>()
                        .orderByDesc("create_time")
                        .last("limit 3")
        );
        List<String> productIdList = productList.stream()
                .map(Product::getProductId)
                .collect(Collectors.toList());
        List<ProductImg> productImgList = productImgService.list(
                new QueryWrapper<ProductImg>()
                        .in("item_id", productIdList)
        );
        Map<String, List<ProductImg>> map = productImgList.stream()
                .collect(Collectors.groupingBy(ProductImg::getItemId));
        productList.forEach(
                product -> {
                    List<ProductImg> productImgs = map.get(product.getProductId());
                    product.setImgs(productImgs == null ? new ArrayList<ProductImg>() : productImgs);
                }
        );
        return ResultVo.ok("查询成功", productList);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryDetailInfo(String productId) {
        Product product = this.getOne(
                new LambdaQueryWrapper<Product>().eq(Product::getProductId, productId)
        );
        List<ProductSku> productSkus = productSkuService.list(
                new LambdaQueryWrapper<ProductSku>()
                        .eq(ProductSku::getProductId, productId)
                        .eq(ProductSku::getStatus, 1)
        );
        List<ProductImg> productImgs = productImgService.list(
                new LambdaQueryWrapper<ProductImg>()
                        .eq(ProductImg::getItemId, productId)
        );
        Map<String, Object> map = new HashMap<>();
        map.put("product", product);
        map.put("productSkus", productSkus);
        map.put("productImgs", productImgs);
        return ResultVo.ok("查询成功", map);
    }

}
